/* Copyright (C) 2014-2015 Mammoth Inc. */

var m = new M("lesson");

$(function() {
	/** DROPDOWN **/
	if (!M.mobile) {
		$(".mm_lesson_title_progress>a").click(function() {
			$(".mm_lesson_context").css("-webkit-filter") === "brightness(0.7)" ? $(".mm_lesson_context").css({
				"-webkit-filter": "brightness(1)"
			}) : $(".mm_lesson_context").css({
				"-webkit-filter": "brightness(0.7)"
			});
		});
		$("body").click(function() {
			$(".mm_lesson_context").css("-webkit-filter") === "brightness(0.7)" && $(".mm_lesson_context").css({
				"-webkit-filter": "brightness(1)"
			});
		});
	}
	/** INITIALIZATION **/
	m.open("conf", M.each($.parseJSON($("#mm_data_conf").html()), decodeURIComponent));
	m.open("text", M.each($.parseJSON($("#mm_data_text").html()), decodeURIComponent));
	m.open("hint", M.each($.parseJSON($("#mm_data_hint").html()), decodeURIComponent));
	m.open("code", M.each(M.each($.parseJSON($("#mm_data_code").html()), decodeURIComponent), function(s) {
		return new RegExp(s);
	}));
	m.open("position", 0);
	var _timemaShow = false;
	/** TEXT **/
	$("#mm_text").html(marked(m.open("text")[0]));
	/** HINT **/
	$("#mm_hint").html(marked(m.open("hint")[0]));
	$("#mm_btn_hint_toggle").click(function() {
		$("#mm_btn_hint_box").css("opacity") == 1 ? $("#mm_btn_hint_box").animate({
			opacity: 0
		}, 300) : $("#mm_btn_hint_box").animate({
			opacity: 1
		}, 300);
	});
	/** TIME MACHINE **/
	$("body").on("click", ".mm_tab_timemachine", function(e) {
		e.preventDefault();
		_timemaShow = true;
		$(this).tab("show");
		$(".mm_timemachine").get(0).contentWindow.window.demoViz.updateOutput();
		$("#mm_console_popout").hide();
	});
	$("#mm_btn_timemachine").click(function() {
		$(".mm_tab_timemachine").click();
	});
	$(".mm_tab_code").click(function() {
		if(_timemaShow) {
			$("#mm_console_popout").show();
		}
	});
	/** CODE EDITOR **/
	m.open("editor", CodeMirror($("#mm_editor")[0], {
		value: m.open("conf")[1],
		mode: m.open("conf")[2] || "",
		theme: "midnight",
		keyMap: "sublime",
		indentUnit: 4,
		smartIndent: true,
		tabSize: 4,
		indentWithTabs: false,
		lineWrapping: true,
		lineNumbers: true,
		matchBrackets: true,
		autoCloseBrackets: true,
		foldGutter: true,
		maxHighlightLength: Infinity,
		gutters: ["CodeMirror-linenumbers", "CodeMirror-foldgutter"],
		extraKeys: {}
	}));
	$('.CodeMirror-vscrollbar').remove();
 	var intvalTime;
	m.open("editor").on('keyup', function (editor,e) {
		clearTimeout(intvalTime);
		if((e.keyCode >= 65 && e.keyCode <= 90) || e.keyCode == 190) {
			intvalTime = setTimeout(function () { 
				CodeMirror.commands.autocomplete(editor); 
			}, 500);
			CodeMirror.commands.autocomplete(editor);
		}else if (e.type == "keyup" && document.querySelector('div.CodeMirror-completions')) {
            var complete = document.querySelector('div.CodeMirror-completions');
	        complete.parentNode.removeChild(complete);
	    }
	});
	/** CODE ENGINE **/
	_update(m.open("editor"));
	m.open("editor").on("change", _update);
	/** CODE EDITOR PANEL **/
	$("#mm_editor_undo").click(function() {
		m.open("editor").undo();
	});
	$("#mm_editor_redo").click(function() {
		m.open("editor").redo();
	});
	$("#mm_editor_reset").click(function() {
		m.open("editor").setValue(m.open("conf")[0]);
	});
	m.open("editor").on("change", function() {
		m.open("editor").historySize().undo === 0 ? $(".mm_undo").css({
			color: "#091d29"
		}) : $(".mm_undo").css({
			color: "#fff"
		});
		m.open("editor").historySize().redo === 0 ? $(".mm_redo").css({
			color: "#091d29"
		}) : $(".mm_redo").css({
			color: "#fff"
		});
	});
	/** QUESTION EDITOR **/
	$("#mm_question").focus(function() {
		$("#mm_addon_count").css({
			"border-right": "1px solid #66afe9",
			"border-top": "1px solid #66afe9",
			"border-bottom": "1px solid #66afe9"
		});
	});
	$("#mm_question").blur(function() {
		$("#mm_addon_count").css({
			"border-right": "1px solid #ffa84b",
			"border-top": "1px solid #ffa84b",
			"border-bottom": "1px solid #ffa84b"
		});
	});
	$("#mm_addon_count").click(function() {
		$("#mm_question").focus();
	});
	$("#mm_question_editor").focus(function() {
		$(".mm_question_editor_tool").css({
			"border-left": "1px solid #66afe9",
			"border-top": "1px solid #66afe9",
			"border-right": "1px solid #66afe9"
		});
	});
	$("#mm_question_editor").blur(function() {
		$(".mm_question_editor_tool").css({
			"border-left": "1px solid #ffa84b",
			"border-top": "1px solid #ffa84b",
			"border-right": "1px solid #ffa84b"
		});
	});
	$("#mm_question_tag").focus(function() {
		$("#mm_addon_tag").css({
			"border-left": "1px solid #66afe9",
			"border-top": "1px solid #66afe9",
			"border-bottom": "1px solid #66afe9"
		});
	});
	$("#mm_question_tag").blur(function() {
		$("#mm_addon_tag").css({
			"border-left": "1px solid #ffa84b",
			"border-top": "1px solid #ffa84b",
			"border-bottom": "1px solid #ffa84b"
		});
	});
	$("#mm_addon_tag").click(function() {
		$("#mm_question_tag").focus();
	});
	$("#mm_question").on("input", function() {
		$("#mm_question_count").html($(this).val().length);
	});
	$("#mm_question_editor").on("input", function() {
		$("#mm_preview").html(marked($(this).val()));
	});
	$("#mm_question_bold").click(function() {
		var _e = $("#mm_question_editor")[0],
			_v = _e.value;
		$("#mm_question_editor").val(_v.substring(0, _e.selectionStart) + "**" + _v.substring(_e.selectionStart, _e.selectionEnd) + "**" + _v.substring(_e.selectionEnd)).trigger("input").focus();
		_e.selectionEnd - _e.selectionStart === 0 ? _e.setSelectionRange(_e.selectionStart + 2, _e.selectionStart + 2) : _e.setSelectionRange(_e.selectionStart, _e.selectionEnd + 4);
	});
	$("#mm_question_italic").click(function() {
		var _e = $("#mm_question_editor")[0],
			_v = _e.value;
		$("#mm_question_editor").val(_v.substring(0, _e.selectionStart) + "_" + _v.substring(_e.selectionStart, _e.selectionEnd) + "_" + _v.substring(_e.selectionEnd)).trigger("input").focus();
		_e.selectionEnd - _e.selectionStart === 0 ? _e.setSelectionRange(_e.selectionStart + 1, _e.selectionStart + 1) : _e.setSelectionRange(_e.selectionStart, _e.selectionEnd + 2);
	});
	$("#mm_question_code").click(function() {
		var _e = $("#mm_question_editor")[0],
			_v = _e.value;
		$("#mm_question_editor").val(_v.substring(0, _e.selectionStart) + "`" + _v.substring(_e.selectionStart, _e.selectionEnd) + "`" + _v.substring(_e.selectionEnd)).trigger("input").focus();
		_e.selectionEnd - _e.selectionStart === 0 ? _e.setSelectionRange(_e.selectionStart + 1, _e.selectionStart + 1) : _e.setSelectionRange(_e.selectionStart, _e.selectionEnd + 2);
	});
	$("#mm_question_quote").click(function() {
		var _e = $("#mm_question_editor")[0],
			_v = _e.value;
		$("#mm_question_editor").val(_v.substring(0, _e.selectionStart) + "\n> " + _v.substring(_e.selectionStart, _e.selectionEnd).replace(/\n\n/g, "\n\n> ") + "\n\n" + _v.substring(_e.selectionEnd)).trigger("input").focus();
		_e.selectionEnd - _e.selectionStart === 0 ? _e.setSelectionRange(_e.selectionStart + 3, _e.selectionStart + 3) : _e.setSelectionRange(_e.selectionStart, _e.selectionEnd + 5);
	});
	$("#mm_question_list").click(function() {
		var _e = $("#mm_question_editor")[0],
			_v = _e.value;
		$("#mm_question_editor").val(_v.substring(0, _e.selectionStart) + "\n\n1. " + _v.substring(_e.selectionStart, _e.selectionEnd).replace(/\n\n/g, "\n\n1. ") + "\n\n" + _v.substring(_e.selectionEnd)).trigger("input").focus();
		_e.selectionEnd - _e.selectionStart === 0 ? _e.setSelectionRange(_e.selectionStart + 5, _e.selectionStart + 5) : _e.setSelectionRange(_e.selectionStart, _e.selectionEnd + 7);
	});
	$("#mm_question_bullet").click(function() {
		var _e = $("#mm_question_editor")[0],
			_v = _e.value;
		$("#mm_question_editor").val(_v.substring(0, _e.selectionStart) + "\n\n- " + _v.substring(_e.selectionStart, _e.selectionEnd).replace(/\n\n/g, "\n\n- ") + "\n\n" + _v.substring(_e.selectionEnd)).trigger("input").focus();
		_e.selectionEnd - _e.selectionStart === 0 ? _e.setSelectionRange(_e.selectionStart + 4, _e.selectionStart + 4) : _e.setSelectionRange(_e.selectionStart, _e.selectionEnd + 6);
	});
});

/** REAL ENGINE **/
function _update(editor) {
	for (var i = 0; i < m.open("code").length; i++) {
		if (m.open("code")[i].test(editor.getValue())) {
			m.open("position", i + 1);
		} else {
			if (i === 0) {
				m.open("position", 0);
			}
			break;
		}
	}
	clearTimeout(window.timer);
	window.timer = setTimeout(function() {
		if (m.open("position") === m.open("code").length) {
			$("#mm_editor_run").data("ok", true);
			$("#mm_editor_run").removeClass("disabled");
		} else {
			$("#mm_editor_run").data("ok", false);
			$("#mm_editor_run").addClass("disabled");
		}
		$("#mm_text").html(marked(m.open("text")[m.open("position")]));
		$("#mm_hint").html(marked(m.open("hint")[m.open("position")]));
		$("#mm_btn_hint_box").css({
			opacity: 0
		});
	}, 400);
}